class LineaArticulosController < ApplicationController
  skip_before_filter :authorize, :only => :create

  # GET /linea_articulos
  # GET /linea_articulos.json
  def index
    @linea_articulos = LineaArticulo.paginate( :page => params[:page],
                                    :per_page => 3)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @linea_articulos }
    end
  end

  # GET /linea_articulos/1
  # GET /linea_articulos/1.json
  def show
    @linea_articulo = LineaArticulo.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @linea_articulo }
    end
  end

  # GET /linea_articulos/new
  # GET /linea_articulos/new.json
  def new
    @linea_articulo = LineaArticulo.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @linea_articulo }
    end
  end

  # GET /linea_articulos/1/edit
  def edit
    @linea_articulo = LineaArticulo.find(params[:id])
  end

  # POST /linea_articulos
  # POST /linea_articulos.json
  def create
    @carrito = current_cart
    producto = Producto.find(params[:producto_id])
    @linea_articulo = @carrito.add_product(producto.id)

    #@linea_articulo = LineaArticulo.new(params[:linea_articulo])

    respond_to do |format|
      if @linea_articulo.save
        format.html { redirect_to(tienda_url)}
        format.js { @current_item = @linea_articulo }
        format.json { render json: @linea_articulo, status: :created, location: @linea_articulo }
      else
        format.html { render action: "new" }
        format.json { render json: @linea_articulo.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /linea_articulos/1
  # PUT /linea_articulos/1.json
  def update
    @linea_articulo = LineaArticulo.find(params[:id])

    if params["linea_articulo"]["cantidad"] == "0"
      destroy
    else
      respond_to do |format|
        if @linea_articulo.update_attributes(params[:linea_articulo])
          format.html { redirect_to :controller => "carritos", :action => "show"}
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @linea_articulo.errors, status: :unprocessable_entity }
        end
      end
    end
end

  # DELETE /linea_articulos/1
  # DELETE /linea_articulos/1.json
  def destroy
    @linea_articulo = LineaArticulo.find(params[:id])
    @linea_articulo.destroy

    respond_to do |format|
      format.html { redirect_to linea_articulos_url }
      format.json { head :no_content }
    end
    def actualizar
      LineaArticulo.update(params[:cantidad].keys, params[:cantidad].values)
    end 
  end
end
